package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.domain.dao.LabelDao;
import com.xbongbong.pro.domain.entity.LabelEntity;
import com.xbongbong.pro.label.pojo.LabelPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.model.LabelModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author guohui.zhang
 * @Title:
 * @date 2020/11/219:01
 */
@Service("labelModel")
public class LabelModelImpl implements LabelModel {
    @Resource
    private LabelDao labelDao;

    @Override
    public Integer insert(LabelEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        return labelDao.insert(entity);
    }

    @Override
    public Integer update(LabelEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return labelDao.update(entity);
    }

    @Override
    public List<LabelEntity> findEntitys(Map<String, Object> param) {
        return labelDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return labelDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer save(LabelEntity entity) throws XbbException {
        if (entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public LabelEntity getByKey(Long key, String corpid) {
        return labelDao.getByKey(key, corpid);
    }

    @Override
    public Integer sortCategory(Map<String, Object> param, Map<Long, Long> sortMap) {
        return labelDao.sort(param, sortMap);
    }

    @Override
    public Integer deleteBykey(Long key, String corpid) {
        return labelDao.deleteByKey(key, corpid);
    }

    @Override
    public List<LabelEntity> getByLabelIds(Collection<Long> ids, String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("ids", ids);
        //按照idIn里面的顺序排序
        param.put("orderByIdIn", ids);
        param.put("columns ", "id,name,color ");
        param.put("del", DelEnum.NORMAL.getDel());
        return labelDao.findEntitys(param);
    }

    @Override
    public List<LabelEntity> getByGroupId(Long groupId, String corpid) {
        return labelDao.getByGroupId(groupId,corpid);
    }

    @Override
    public Integer updateBatch(List<LabelEntity> labelEntities, String corpid) {
        return labelDao.updateBatch(labelEntities, corpid);
    }

    @Override
    public List<LabelEntity> getByName(String name, Long formId, String corpid) {
        return labelDao.getByName(name,formId, corpid);
    }

    @Override
    public void insertBatch(List<LabelEntity> labelEntities, String corpid) {
        labelDao.insertBatch(labelEntities);
    }

    @Override
    public void deleteBatch(List<Long> labelIds, String corpid) {
        labelDao.deleteBatch(labelIds, corpid);
    }

    @Override
    public void deleteByFormIds(Collection<Long> systemFormList, String corpid, Long appId) {
        labelDao.deleteByFormIds(systemFormList, corpid, appId);
    }



    @Override
    public Map<Long, LabelPojo> dealLabel(Long formId, Long appId, String corpid, Collection labelsIdList) {
        if (CollectionUtils.isEmpty(labelsIdList)) {
            return new HashMap<>(BasicConstant.ZERO);
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("formId", formId);
        param.put("appId", appId);
        param.put("del", DelEnum.NORMAL.getDel());
        List<LabelEntity> entitys = findEntitys(param);
        Map<Long, LabelPojo> nameToLabel = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        entitys.forEach(item->nameToLabel.put(item.getId(), new LabelPojo(item.getId(),item.getColor(),item.getName())));

        return nameToLabel;
    }
}
